import 'dart:convert';

import 'package:appflutterhomeschool/utils/sp_util.dart';
import 'package:logger/logger.dart';

///两个接口统一数据
class StudentInformationModel {
  String imageUrl;
  String imageUrlFalse;

  //学生信息 年级  学校  姓名
  //班级信息  年级 班级  科目   学校  代码
  //学生姓名
  String userName;

  //名字  班级名称
  String className;

  //科目
  String subject;

  //学校
  String school;

  //班级代码
  String code;

  //学生学号
  String studentNo;

  //班级信息下面三个代码  class_id  class_mapping_id subject_id
  //班级id
  String classId;
  String classMappingId;

  //科目id
  String subjectId;

  //额外参数  是否选中
  bool choice;

  //年级
  String grade;

  //学生展示的科目列表
  List subjects;

  StudentInformationModel(
      {this.userName,
      this.classId,
      this.classMappingId,
      this.choice,
      this.className,
      this.studentNo,
      this.subjectId,
      this.subject,
      this.school,
      this.code});

  StudentInformationModel.fromJson1(Map<String, dynamic> json) {
    choice = json['choice'];
    classMappingId = json['class_mapping_id'];
    //姓名
    userName = json['userName'];
    studentNo = json['studentNo'];
    //班级名字
    className = json['name'];
    //科目
    subject = json['subject'];
    //学校
    school = json['school'];
    //代码
    code = json['code'];
    //班级id
    classId = json['class_id'];
    //科目id
    subjectId = json['subject_id'];
    grade = json['grade'];
  }

  StudentInformationModel.fromJson(
      bool swidtchBool, Map<String, dynamic> json) {
    if (json["subjects"] != null) {
      subjects = json["subjects"] as List;
      Logger().d(subjects.toList());
    }
    choice = false;
    //科目id
    print("======${json['grade']}");
    grade = "${json['grade'] ?? ""}";
    classMappingId = "${json['class_mapping_id'] ?? ""}";
    studentNo = "${json['student_no'] ?? ""}";
    //科目id
    subjectId = "${json['student_id'] ?? ""}";
    //学校
    school = json['school'] ?? "";
    //科目
    subject = "${json['subject'] ?? ""}";
    //代码
    code = json['code'] ?? "";
    if (swidtchBool) {
      subjectId = json['subject_id'];
      //姓名
      userName = "";
      //班级名字
      className = json['name'];
      //科目
      subject = "${json['subject']}";
      //班级id
      classId = "${json['class_id']}";
    } else {
      //姓名
      userName = json['name'] ?? "";
      //班级名字
      className = json['class_name'] ?? "";
      //班级id
      classId = "${json['cls_id']}";
    }
    String getStudent = SpUtil.getString("getStudent") ?? null;

    if (getStudent.isNotEmpty && getStudent != null) {
      Map<String, dynamic> mapJson = jsonDecode(getStudent);

      if (classId == mapJson["class_id"]) {
        choice = true;
      }
    }
  }

  StudentInformationModel.fromJsonLogin(Map<String, dynamic> json) {
//    "name": "测试班级1",
//    "subject": "化学",
//    "cls_mapping_id": 2
    userName = json['name'] ?? json['cls_name'];
    className = json['cls_name'];
    classMappingId = "${json['cls_mapping_id']}";
    subjectId = "${json['student_id']}";
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['choice'] = this.choice;
    data['class_mapping_id'] = this.classMappingId;
    data['userName'] = this.userName;
    data['name'] = this.className;
    data['subject'] = this.subject;
    data['school'] = this.school;
    data['code'] = this.code;
    data['class_id'] = this.classId;
    data['subject_id'] = this.subjectId;
    data['studentNo'] = this.studentNo;
    return data;
  }
}

class StudentModel {
  String name;
  String img;
  String studentId;

  //额外参数  是否选中
  bool choice;

  StudentModel({this.name, this.img, this.studentId, this.choice});

  StudentModel.fromJson(bool swidtchBool, Map<String, dynamic> json) {
    choice = false;
    img = json['img'];
    studentId = "${json['student_id'] ?? ""}";
    name = json['name'] ?? "";
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['img'] = this.img;
    data['student_id'] = this.studentId;
    data['name'] = this.name;
    data['choice'] = this.choice;
    return data;
  }
}
